#include <modules\graphics\graphics_interface.h>

namespace DelGE
{
	namespace Modules
	{
		namespace Graphics
		{
			GraphicsInterface::GraphicsInterface()
			{
				this->pCurrentContext = NULL;
				this->pCurrentRenderBuffer = NULL;
			}

			GraphicsInterface::~GraphicsInterface()
			{
				this->pCurrentContext = NULL;
				this->pCurrentRenderBuffer = NULL;
				this->DestroyAllContexts();
			}

			void GraphicsInterface::AddContext(GraphicsContext* pContext)
			{
				list<GraphicsContext*>::iterator itContext = find(this->lContexts.begin(), this->lContexts.end(), pContext);
				if (itContext == this->lContexts.end())
					this->lContexts.push_back(pContext);
				// else the list already contains the context passed.
			}

			void GraphicsInterface::RemoveContext(GraphicsContext* pContext)
			{
				this->lContexts.remove(pContext);
			}

			void GraphicsInterface::SetCurrentContext(GraphicsContext* pContext)
			{
				list<GraphicsContext*>::iterator itContext = find(this->lContexts.begin(), this->lContexts.end(), pContext);
				if (itContext != this->lContexts.end())
				{
					this->pCurrentContext = pContext;
					this->pCurrentContext->MakeCurrent();
				}
				// else the list doesnt contain the context passed.
			}

			void GraphicsInterface::DestroyAllContexts()
			{
				GraphicsContext* pContext;
				list<GraphicsContext*>::iterator itContext;

				for (itContext = this->lContexts.begin(); itContext != this->lContexts.end(); ++itContext)
				{
					pContext = ((GraphicsContext*)(*itContext));
					delete pContext;
				}

				this->lContexts.clear();
			}

			GraphicsContext *GraphicsInterface::GetCurrentContext()
			{
				return this->pCurrentContext;
			}

			int GraphicsInterface::GetClearColor_Red()
			{
				return this->oClearColor[0];
			}

			int GraphicsInterface::GetClearColor_Green()
			{
				return this->oClearColor[1];
			}

			int GraphicsInterface::GetClearColor_Blue()
			{
				return this->oClearColor[2];
			}
			
			int GraphicsInterface::GetClearColor_Alpha()
			{
				return this->oClearColor[3];
			}

			Color GraphicsInterface::GetClearColor()
			{
				return this->oClearColor;
			}

			int GraphicsInterface::GetGlobalColor_Red()
			{
				return this->oGlobalColor[0];
			}

			int GraphicsInterface::GetGlobalColor_Green()
			{
				return this->oGlobalColor[1];
			}

			int GraphicsInterface::GetGlobalColor_Blue()
			{
				return this->oGlobalColor[2];
			}
			
			int GraphicsInterface::GetGlobalColor_Alpha()
			{
				return this->oGlobalColor[3];
			}

			Color GraphicsInterface::GetGlobalColor()
			{
				return this->oGlobalColor;
			}

			DelGEGraphicsBlendMode GraphicsInterface::GetBlendMode()
			{
				return this->eBlendMode;
			}

			Matrix GraphicsInterface::GetModelViewMatrix()
			{
				return this->oModelViewMatrix;
			}

			Matrix GraphicsInterface::GetProjectionMatrix()
			{
				return this->oProjectionMatrix;
			}

			void GraphicsInterface::PushWorldMatrix()
			{
				this->lMatrixStack.push(this->oModelViewMatrix);
			}

			void GraphicsInterface::PopWorldMatrix()
			{
				this->oModelViewMatrix = this->lMatrixStack.top();
				this->lMatrixStack.pop();

				this->SetModelViewMatrix(this->oModelViewMatrix);
			}
		}
	}
}